package com.xc.controller;

import com.xc.common.ServerResponse;
import com.xc.pojo.SiteSetting;
import com.xc.service.ISiteAdminService;
import com.xc.service.ISiteSettingService;
import com.xc.utils.PropertiesUtil;
import com.xc.utils.redis.CookieUtils;
import com.xc.utils.redis.JsonUtil;
import com.xc.utils.redis.RedisConst;
import com.xc.utils.redis.RedisShardedPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/26 11:30 AM
 */
@Controller
@RequestMapping("/api/admin/")
@Slf4j
public class AdminApiController {

    @Autowired
    ISiteAdminService iSiteAdminService;
    @Autowired
    ISiteSettingService iSiteSettingService;

    /**
     *  管理员登陆
     * @param adminPhone
     * @param adminPwd
     * @param verifyCode
     * @param httpSession
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("login.do")
    @ResponseBody
    public ServerResponse login(@RequestParam("adminPhone") String adminPhone,
                                     @RequestParam("adminPwd") String adminPwd,
                                     @RequestParam("verifyCode") String verifyCode,
                                     HttpSession httpSession,
                                     HttpServletRequest request,
                                     HttpServletResponse response){

        ServerResponse serverResponse = iSiteAdminService
                .login(adminPhone,adminPwd,verifyCode,request);

        String admin_cookie_name = PropertiesUtil.getProperty("admin.cookie.name");

        if(serverResponse.isSuccess()){  //  登陆成功
            //  往浏览器发送一个logintoken
            CookieUtils.writeLoginToken(response
                    , RedisConst.getAdminRedisKey(httpSession.getId()), admin_cookie_name);
            //  在redis服务器上保存用户的登陆信息
            String redisSetExResult = RedisShardedPoolUtils
                    .setEx(RedisConst.getAdminRedisKey(httpSession.getId())
                            , JsonUtil.obj2String(serverResponse.getData()),
                            RedisConst.RedisCacheExTime.REDIS_SESSION_EXTIME);
            log.info("redis setex admin result : {}",redisSetExResult);
        }

        return serverResponse;
    }

    /**
     *  管理员退出登录
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @RequestMapping("logout.do")
    @ResponseBody
    public ServerResponse logout(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse){
        String cookie_name = PropertiesUtil.getProperty("admin.cookie.name");
        String logintoken = CookieUtils.readLoginToken(httpServletRequest,cookie_name);
        log.info("管理员 token = {} ,退出登陆",logintoken);
        //  删除redis服务器上的logintoken
        RedisShardedPoolUtils.del(logintoken);
        //  删除cookie
        CookieUtils.delLoginToken(httpServletRequest,httpServletResponse,cookie_name);
        return ServerResponse.createBySuccess();
    }


    /**
     *  管理员 邮件 审核充值订单
     * @param token     email 的token
     * @param state     充值订单的状态
     * @param orderSn   订单编号
     * @return
     */
    @RequestMapping("authCharge.do")
    @ResponseBody
    public ServerResponse authCharge(@RequestParam("token") String token,
                                @RequestParam("state") Integer state,
                                @RequestParam("orderSn") String orderSn){
        return iSiteAdminService.authCharge(token,state,orderSn);
    }

    /**
     *  获取网站设置信息
     * @return
     */
    @RequestMapping("getSetting.do")
    @ResponseBody
    public ServerResponse getSetting(){
        return ServerResponse.createBySuccess(iSiteSettingService.getSiteSetting());
    }





}
